<?php namespace system\model;


use Hdphp\Model\Model;

class Tag extends Model{
    //数据表
    protected $table = 'tag';
    //自动验证
    protected $validate = [
        //['字段名','验证方法','提示信息',验证条件,验证时间]
        ['tname','required','请输入标签名',3.3],
    ];
    public function getAll(){
        //分页   统计一共多少条数据 按照每页6条分
       $page = Page::row(6)->make($this->count());
        //
        $data = $this->limit(page::limit())->get();
        return ['page'=>$page,'data'=>$data];
    }
    //添加功能
    public function store(){
        if(empty($_POST['tname'])){
            $this->error = '标签不能为空';
            return false;
        }
        //将接受的字符串转为数组
        $tname = explode('|',$_POST['tname']);
        //p($tname);exit;
        foreach ($tname as $k => $v){
            //如果提交的标签数据库已经存在
            if($this->where('tname',$v)->first()){
                //把数据库里和提交标签一样的删除
                $this->where('tname',$v)->delete();
            }
            $this->insert(['tname'=>$v]);
            //var_dump($a);
        }

        return true;
    }
    //编辑
    public function edit(){

        if(!$this->create()) return false;
        //执行修改
       return $this->save();
    }
}


